package com.yksj.healthtalk.ui.doctorstation;

import java.util.ArrayList;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorServiceContentListAdapter;
import com.yksj.healthtalk.adapter.DoctorServiceContentListAdapter.OnClickDeleteListener;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.DoctorServiceContentEntity;
import com.yksj.healthtalk.net.http.AsyncHttpResponseHandler;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.utils.ToastUtil;
/**
 * 已删除服务内容列表
 * @author Administrator
 *
 */
public class DoctorServiceContentDeletedListActivity extends BaseFragmentActivity implements OnClickListener,OnClickDeleteListener{
	private ListView mListView;
	private PullToRefreshListView refreshListView;
	private List<DoctorServiceContentEntity> entities=new ArrayList<DoctorServiceContentEntity>();
	private DoctorServiceContentListAdapter adapter;
	private String SERVICE_ITEM_ID="";

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_service_content_deleted_layout);
		initView();
	}

	private void initView() {
		initTitle();
		if(getIntent().hasExtra("SERVICE_ITEM_ID"))
		SERVICE_ITEM_ID = getIntent().getStringExtra("SERVICE_ITEM_ID");
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("已删除服务");
		titleRightBtn2.setOnClickListener(this);
		titleRightBtn2.setText("还原");
		titleRightBtn2.setVisibility(View.VISIBLE);
		
		refreshListView = (PullToRefreshListView)findViewById(R.id.listview_fragment);
		mListView = refreshListView.getRefreshableView();
		adapter = new DoctorServiceContentListAdapter(entities,this,this,1);
		mListView.setAdapter(adapter);
		initData();
		
		
	}
	private void initData() {
		HttpRestClient.doHttpQueryDoctorContentDeleted(SERVICE_ITEM_ID,new ObjectHttpResponseHandler(this) {
			@Override
			public Object onParseResponse(String content) {
				return DoctorServiceContentEntity.parsoToList(content);
			}
			@Override
			public void onSuccess(Object response) {
				super.onSuccess(response);
				if(response!=null){
					adapter.onBoundData((List<DoctorServiceContentEntity>)response);
				}else{
				}
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			setResult(RESULT_OK, getIntent());
			finish();
			overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
			break;
		case R.id.title_right2://还原
		int size=adapter.entities.size();
			JSONArray array=new JSONArray();
			DoctorServiceContentEntity entity;
			for (int i = 0; i <size; i++) {
				entity=adapter.entities.get(i);
				if("1".equals(entity.getISSELECT())){
					array.put(entity.getSERVICE_CONTENT_ID());
				}
			}
			if(array.length()<=0)return;
			submint(array.toString());
			break;
		}
	}
	
	//选择服务内容提交
	private void submint(String parame) {
		HttpRestClient.doHttpDoctorServiceReductionContent(SERVICE_ITEM_ID,parame,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, String content) {
				super.onSuccess(statusCode, content);
				try {
					JSONObject object=new JSONObject(content);
					if(object.has("error_message")){
						ToastUtil.showBasicShortToast(getApplicationContext(), object.optString("error_message")+"");
					}else{
						setResult(RESULT_OK,getIntent());
						finish();
					}
				} catch (JSONException e) {
				}
			}
		});
	}
	
	//删除
	@Override
	public void onclick(final DoctorServiceContentEntity entity) {
	}
}
